﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using System.IO;

namespace RunVistaGadget31.AdditionalApplicationsWrappers
{
    /// <summary>
    /// 
    /// </summary>
    public class ApplicationRunner
    {
        private bool _IsRunAsWinApp;
        private String _ApplicationArgs = String.Empty;
        /// <summary>
        /// 
        /// </summary>
        public String GadgetPath;
        /// <summary>
        /// 
        /// </summary>
        protected String CalledApplicationPath = String.Empty;
        /// <summary>
        /// 
        /// </summary>
        protected EnvDTE.Project project;
        static GadgetProperties gp;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationObject"></param>
        public ApplicationRunner(DTE2 applicationObject)
            : this(applicationObject, false)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationObject"></param>
        /// <param name="isRunAsWinApp"></param>
        public ApplicationRunner(DTE2 applicationObject, bool isRunAsWinApp)
        {
            if (applicationObject.Solution.Projects.Count > 0)
            {
                project = applicationObject.Solution.Projects.Item(1);
            }
            _IsRunAsWinApp = isRunAsWinApp;
            GadgetPath = "\"" + GadgetProp.GadgetOutputPath + "\"";
        }

        /// <summary>
        /// 
        /// </summary>
        public static GadgetProperties GadgetProp
        {
            get
            {
                if (gp == null)
                    gp = new GadgetProperties();
                return gp;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual String GetAppArguments()
        {
            return String.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual String GetOutputAndRunApplication()
        {
            this._ApplicationArgs = GetAppArguments();
            return GetCommonOutputAndRunApplication();
        }

        private String GetCommonOutputAndRunApplication()
        {
            System.Diagnostics.Process AppProcess = new System.Diagnostics.Process();
            if (_IsRunAsWinApp == false)
            {
                AppProcess.StartInfo.UseShellExecute = false;
                AppProcess.StartInfo.RedirectStandardOutput = true;
                AppProcess.StartInfo.CreateNoWindow = true;
                AppProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(CalledApplicationPath);
            }
            AppProcess.StartInfo.Arguments = _ApplicationArgs;

            AppProcess.StartInfo.FileName = CalledApplicationPath;

            AppProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

            String ApplicationOutput = String.Empty;
            try
            {
                bool result = AppProcess.Start();

                if (_IsRunAsWinApp == false)
                {
                    AppProcess.WaitForExit();
                    ApplicationOutput = AppProcess.StandardOutput.ReadToEnd();
                }
                else
                {
                    if (result == true)
                    {
                        ApplicationOutput = "Application has been run sucessfully.";
                    }
                    else
                    {
                        ApplicationOutput = "Could not start application.";
                    }
                }
                AppProcess.Close();
            }
            catch (Exception ex)
            {
                ApplicationOutput = String.Format("Exception has occured. Exception type:{0} \nException message: {1} {1} \nException stack trace: \n{2} ", ex.Source, ex.Message, ex.StackTrace);
            }
            return ApplicationOutput;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationPath"></param>
        /// <param name="applicationName"></param>
        /// <returns></returns>
        public bool IsExistCalledApplication(string applicationPath, string applicationName)
        {
            bool IsExistApplication = false;
            string TestAppExistanceRegexExpression = @"\\" + applicationName;
            System.Text.RegularExpressions.Regex TestAppExistance = new System.Text.RegularExpressions.Regex(
                TestAppExistanceRegexExpression, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (TestAppExistance.IsMatch(applicationPath))
            {
                IsExistApplication = true;
            }

            return IsExistApplication;
        }

    }
}
